package com.buba.system.dao.impl;

import com.buba.system.dao.LoginLogDao;
import com.buba.system.pojo.LoginLog;
import com.buba.system.pojo.UserInfo;
import com.buba.util.DBPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class LoginLogDaoImpl implements LoginLogDao {
    QueryRunner queryRunner = new QueryRunner(DBPool.getDS());


    @Override
    public void loginlog(Map<String, String> param, UserInfo userInfo, String ipAddr) {

        String sql = "insert into LoginLog values(null,?,?,?,?,?)";
        //给占位符赋值
        Object[] objects = new Object[5];
        objects[0] = userInfo.getUserId();
        objects[1] = param.get("tima");
        objects[2] = "1";
        objects[3] = ipAddr;
        objects[4] = userInfo.getUserName();
        try {
            queryRunner.update(sql,objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 登录失败的方法
     * @param param
     * @param ipAddr
     */
    @Override
    public void loginlog(Map<String, String> param, String ipAddr) {
        String sql = "insert into LoginLog values(null,?,?,?,?,?)";
        //给占位符赋值
        Object[] objects = new Object[5];
        objects[0] = 0;
        objects[1] = param.get("tima");
        objects[2] = "0";
        objects[3] = ipAddr;
        objects[4] = param.get("account");
        try {
            queryRunner.update(sql,objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 查询
     * @param param
     * @return
     */
    @Override
    public List<LoginLog> selLogin(Map<String, String> param) {
        String sql = "select * from loginlog where 1=1";
        String datetimeBegin = param.get("datetimeBegin");
        String datetimeFinish = param.get("datetimeFinish");
        if (datetimeBegin != null && !datetimeBegin.trim().equals("")){
            sql += " and LoginTime >=" + "'"+datetimeBegin+"'" ;
        }
        if (datetimeFinish != null && !datetimeFinish.trim().equals("")){
            sql += " and LoginTime <=" + "'"+datetimeFinish+"'" ;
        }
        try {
            return queryRunner.query(sql,new BeanListHandler<>(LoginLog.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int delLogin(String loginId) {
        String sql = "delete from loginlog where LoginId in (" + loginId + ")";
        try {
            return queryRunner.update(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

//    /**
//     * 删除
//     * @param param
//     * @return
//     */
//    @Override
//    public int delLogin(Map<String, String> param) {
//        String sql = "delete from loginlog where LoginId="+param.get("loginId");
//        try {
//            return queryRunner.update(sql);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//        return 0;
//    }
}
